package com.ks.repair.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.OperateBase;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.exception.GunsException;
import com.ks.core.util.ToolUtil;
import com.ks.repair.entity.BRepairClass;
import com.ks.repair.dao.BRepairClassMapper;
import com.ks.repair.service.IBRepairClassService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.repair.service._ServiceBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报修-每个租户维护的自己的报修类目清单 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-16
 */
@Service
public class BRepairClassServiceImpl extends _ServiceBase<BRepairClassMapper, BRepairClass> implements IBRepairClassService {
    @Resource
    private BRepairClassMapper bRepairClassMapper;

    @Override
    public Map<String, Object> selectRepairClass(OperationContext context,SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BRepairClass> wrapper =new EntityWrapper<>();
        BRepairClass bRepairClass  =new BRepairClass();
        if(ToolUtil.isNotEmpty(fields)){
            for(SearchCriteriaBase.matchField field:fields){
                if("id".equals(field.getName())){
                    bRepairClass.setTenantID(Integer.getInteger(field.getValue()));
                }
            }
        }
        if(!isSystem(context)){
            bRepairClass.setTenantID(context.getTenantId().intValue());
        }
        bRepairClass.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(bRepairClass);
        Page limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BRepairClass> bRepairClasses = bRepairClassMapper.selectPage(limit,wrapper);
        Map<String, Object>  resultMap = this.isResultMap(bRepairClasses,searchCriteriaBase);
        Integer count = bRepairClassMapper.selectCount(wrapper);
        resultMap.put("count",count);
        return resultMap;
    }

    @Override
    public int addRepairClass(OperationContext context,BRepairClass bRepairClass) {
        EntityWrapper<BRepairClass> wrapper =new EntityWrapper<>();
        BRepairClass Repair  =new BRepairClass();
        Repair.setINumber(bRepairClass.getINumber());
        if(!isSystem(context)){
            bRepairClass.setTenantID(context.getTenantId().intValue());
            bRepairClass.setOrgID(context.getOrgId().intValue());
            bRepairClass.setOrgSubID(context.getOrgSubId().intValue());
            bRepairClass.setDataType("TENANT");
            Repair.setTenantID(context.getTenantId().intValue());
            Repair.setOrgID(context.getOrgId().intValue());
            Repair.setOrgSubID(context.getOrgSubId().intValue());
        }
        bRepairClass.setCreateUserID(context.getUserId().intValue());
        wrapper.setEntity(Repair);
        List<BRepairClass> bRepairClasses = bRepairClassMapper.selectList(wrapper);
        if(bRepairClasses.size()>0){
            throw new GunsException("编码不能重复!");
        }
        Integer i = bRepairClassMapper.insert(bRepairClass);
        return i;
    }

    @Override
    public int updateRepairClass(OperationContext context,BRepairClass bRepairClass) {
        Integer i = bRepairClassMapper.updateById(bRepairClass);
        return i;
    }

    @Override
    public int deleteRepairClass(OperationContext context, OperateBase operateBase) {
        int count = 0;
        ArrayList<String> idList = operateBase.getIdList();
        for(String id:idList){
            BRepairClass bRepairClass = bRepairClassMapper.selectById(id);
            bRepairClass.setDelMark(Const.DEL_FLAG_DEL);
            Integer i = bRepairClassMapper.updateById(bRepairClass);
            count+=i;
        }
        return count;
    }


    public Map<String, Object>  isResultMap(List list, SearchCriteriaBase searchCriteriaBase){
        Map<String, Object>  map = new HashMap<>();
        map.put("count",list.size());
        map.put("limit",searchCriteriaBase.getLimit());
        map.put("pageIndex",searchCriteriaBase.getPageIndex());
        map.put("hasMore",false);
        map.put("items",list);
        return  map;
    }
}
